<?php
class AssistantsController extends AppController {

	var $name = 'Assistants';
	var $helpers = array('Html', 'Form');

	function index() 
    {
        $this->set('choose1',33);
        if($this->Session->read('User.type')=='jobseeker')
        {
            if(!empty($this->data))
            {
                //var_dump($this->data);
                //exit;

                if($this->data['Field'])
                {
                    $query = "DELETE FROM `assistants_fields` WHERE assistant_id = ".$this->data['Assistant']['id'];
                    $this->Assistant->query($query);                
                    foreach($this->data['Field']['Field'] as $temp)
                    {
                        $query = "INSERT INTO `assistants_fields` (assistant_id,field_id) VALUES (".$this->data['Assistant']['id'].", ".$temp.")";
                        $this->Assistant->query($query);
                    }
                }

                if($this->data['City'])
                {
                    $query = "DELETE FROM `assistants_cities` WHERE assistant_id = ".$this->data['Assistant']['id'];
                    $this->Assistant->query($query);                
                    foreach($this->data['City']['City'] as $temp)
                    {
                        $query = "INSERT INTO `assistants_cities` (assistant_id,city_id) VALUES (".$this->data['Assistant']['id'].", ".$temp.")";
                        $this->Assistant->query($query);
                    }
                }
                // if(isset($_POST["salary"]) && isset($_POST["skill"])){
					$query = "DELETE FROM `assistant_priorities` WHERE assistant_id = ".$this->data['Assistant']['id'];
                    $this->Assistant->query($query);                
                    $query = "INSERT INTO `assistant_priorities` (assistant_id,type,priority) VALUES (".$this->data['Assistant']['id'].", '1', '" . $_POST["salary"] . "')";
                    mysql_query($query);
					$query = "INSERT INTO `assistant_priorities` (assistant_id,type,priority) VALUES (".$this->data['Assistant']['id'].", '2', '" . $_POST["skill"] . "')";
                    mysql_query($query);
					$query = "INSERT INTO `assistant_priorities` (assistant_id,type,priority) VALUES (".$this->data['Assistant']['id'].", '3', '" . $_POST["reputation"] . "')";
                    mysql_query($query);
				// }
                $this->data['Assistant']['status']=1;
                $this->Assistant->save($this->data['Assistant']);
                $this->Session->setFlash(__('Your assistant profile has been changed', true));
                $this->redirect(array('controller'=>'jobseekers', 'action'=>'index'),null, true);
            }
            if (empty($this->data))
            {
                //$this->Assistant->recursive = -1;
                $this->data = $this->Assistant->findByJobseekerId($this->Session->read('User.id'));
            }
            $academics = $this->Assistant->Academic->find('list'); 
            $cities = $this->Assistant->City->find('list',array('order'=>'City.name')); 
            $fields = $this->Assistant->Field->find('list',array('order'=>'Field.name'));
            $assistant_priorities = $this->Assistant->Assistant_priority->find('list');
			// var_dump($this->data);
			// var_dump($fields);
            $this->set(compact('cities','fields','academics')); 

        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
        }
	}
    
    function result()
    {                  
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->set('choose1',34);
            $this->Assistant->recursive=-1;
            $assistant = $this->Assistant->find('first',array('conditions'=>array('Assistant.jobseeker_id'=>$this->Session->read('User.id'))));
            
            $assistant = $assistant['Assistant'];
            
            if($assistant['status']!=0)
            {
                $query = "SELECT distinct(jobs.id) FROM `jobs`,`fields_jobs` WHERE fields_jobs.job_id =jobs.id ";
                $query .= " OR fields_jobs.field_id IN ( SELECT assistants_fields.field_id FROM `assistants_fields` WHERE assistants_fields.assistant_id = ".$assistant['id'].")";         
                $query .= " OR jobs.city_id IN ( SELECT assistants_cities.city_id FROM `assistants_cities` WHERE assistants_cities.assistant_id  = ".$assistant['id'].")";         
                $query .= " OR jobs.position LIKE '%".$assistant['keywords']."%'";
                $query .= " OR jobs.academic_id <= ".$assistant['academic_id'];
                $query .= " OR jobs.experience <= ".$assistant['experience'];
                
                $temp = 0;
                if ( $assistant['salary'] >= MIN_SALARY_4  )
                { $temp  = 4; }
                if ( $assistant['salary'] <= MAX_SALARY_3  )
                { $temp  = 3; }
                if ( $assistant['salary'] <= MAX_SALARY_2  )
                { $temp  = 2; }
                if ( $assistant['salary'] <= MAX_SALARY_1  )
                { $temp  = 1; }

                if ( $assistant['negotiable'] == 1  )
                { $query .= " OR ( jobs.salary >= ".$temp." OR jobs.salary = 0 )"; }
                else
                { $query .= " OR ( jobs.salary >= ".$temp." OR jobs.salary <> 0)"; }             
                
                $temp = " OR ((jobs.parttime = 1 AND jobs.parttime = ".$assistant['parttime'].")";
                $temp .= " OR (jobs.fulltime = 1 AND jobs.fulltime = ".$assistant['fulltime'].")";
                $temp .= " OR (jobs.freelance = 1 AND jobs.freelance = ".$assistant['freelance'].")";
                $temp .= " OR (jobs.contract = 1 AND jobs.contract = ".$assistant['contract'].")";
                $temp .= " OR (jobs.temporary = 1 AND jobs.temporary = ".$assistant['temporary'].") )";
                
                if ( $assistant['parttime'] || $assistant['fulltime'] || $assistant['freelance'] || $assistant['contract'] || $assistant['temporary'] )
                { $query .= $temp; }


                $temp = " AND ( (jobs.entry = 1 AND jobs.entry = ".$assistant['entry'].")";
                $temp .= " OR (jobs.middle = 1 AND jobs.middle = ".$assistant['middle'].")";
                $temp .= " OR (jobs.senior = 1 AND jobs.senior = ".$assistant['senior'].")";
                $temp .= " OR (jobs.top = 1 AND jobs.top = ".$assistant['top'].") )";

                if ( $assistant['entry'] || $assistant['middle'] || $assistant['senior'] || $assistant['top'] )
                { $query .= $temp; }
                
                $query2="SELECT jobs.id, jobs.position, jobs.created, jobs.salary, jobs.experience, jobs.entry, jobs.middle, jobs.senior, jobs.top, employers.id, employers.company_name, cities.name FROM `jobs`,`employers`,`cities` WHERE jobs.employer_id = employers.id AND cities.id = jobs.city_id AND jobs.status=1 AND jobs.end_date >='".date("Y-m-d")."' AND jobs.id IN (".$query.") ORDER BY jobs.created DESC,jobs.id DESC";
                // $jobs = $this->Assistant->query($query_ku);
				if ( $assistant['negotiable'] == 0  )
                { $nego = "AND jobs.salary <> 0 "; }
                else
                { $nego = ""; }
				 $query2="SELECT
						jobs.id, jobs.position, jobs.created, jobs.salary, jobs.experience, jobs.entry, jobs.middle, jobs.senior, jobs.top, employers.id, employers.company_name, cities.name,
						jobs.id,
						jobs.position,
						employers.company_name,
						employers.reputation AS score_reputation,
						COUNT(jobseeker_skills.skill_id) AS score_skill,
						jobs.salary,
						COUNT(assistants_cities.city_id),
						IF(assistants.salary >= 1000000 OR assistants.salary <= 2000000, 1,IF(assistants.salary > 2000000 OR assistants.salary <= 3000000,2,IF(assistants.salary > 3000000 OR assistants.salary <= 5000000,3, IF(assistants.salary > 5000000, 4, 0)))) as expected_salary,
						IF(assistants.negotiable = 0, 'no', 'yes') as include_negotiable
						FROM jobs
						JOIN employers ON employers.id = jobs.employer_id
						JOIN job_requirements ON job_requirements.job_id = jobs.id
						LEFT JOIN jobseeker_skills ON jobseeker_skills.skill_id = job_requirements.skill_id JOIN assistants ON assistants.jobseeker_id = jobseeker_skills.jobseeker_id
						LEFT JOIN assistants_cities ON assistants_cities.city_id = jobs.city_id AND assistants_cities.assistant_id = '" . $assistant['id'] . "'
						JOIN cities ON assistants_cities.city_id = cities.id
						WHERE jobseeker_skills.jobseeker_id = '" . $this->Session->read('User.id') . "' AND assistants.jobseeker_id = '" . $this->Session->read('User.id') . "' " . $nego ."GROUP BY jobs.id";
                $jobs = $this->Assistant->query($query2);
                // echo $query2;
                $this->set('jobs',$jobs);
				// echo $query;
            }
			$to = "zulmi.adi.rizki@gmail.com";
			// $to = $this->data['Jobseeker']['email'];
			include("PHPMailer/class.phpmailer.php");
			include("PHPMailer/class.smtp.php"); // note, this is optional - gets called from main class if not already loaded

			$mail             = new PHPMailer();

			$mail->IsSMTP();
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
			$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
			$mail->Port       = 465;                   // set the SMTP port

			$mail->Username   = "itjobz.indo@gmail.com";  // GMAIL username
			$mail->Password   = "1qaz2wsx3ec";            // GMAIL password

			$mail->From       = "itjobz.indo@gmail.com";
			$mail->FromName   = "Webmaster";
			$mail->Subject    = "Jobseeker Member Activation";
			$mail->AltBody    = "Jobseeker Member Activation"; //Text Body
			$mail->WordWrap   = 50; // set word wrap
			// $link = "http://www.itjobz.jux.in/jobseekers/verification/".$newId."/".$this->data['Jobseeker']['codes'];
			$body = file_get_contents("http://localhost/it-jobz/assistants/result");   
			// $body = "test";   
			$mail->MsgHTML($body);

			$mail->AddReplyTo("itjobz.indo@gmail.com","Webmaster");

			$mail->AddAddress($to,"First Last");

			$mail->IsHTML(true); // send as HTML

			// $mail->Send();
			// if(!$mail->Send()) {
			  // echo "Mailer Error: " . $mail->ErrorInfo;
			// } else {
			  // echo "Message has been sent";
			// }
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);
        }    
    }

}
?>
